<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/static/crm/lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="/static/crm/css/public.css" media="all">
    <link rel="stylesheet" href="/static/crm/lib/font-awesome-4.7.0/css/font-awesome.min.css" media="all">
    <style>
        .layui-table th {
            font-weight: bold;
            color: #333333;
            text-align: center;
        }

        .layui-table {
            text-align: center;
        }
    </style>
</head>

<body>
    <div class="layuimini-container">
        <div class="layuimini-main">

            <fieldset class="table-search-fieldset" style="display: none;">
                <legend>搜索信息</legend>
                <div style="margin: 10px 10px 10px 10px">
                    <form class="layui-form layui-form-pane" action="">
                        <div class="layui-form-item">
                            <div class="layui-inline">
                                <label class="layui-form-label">校区名称</label>
                                <div class="layui-input-inline">
                                    <input type="text" name="name" autocomplete="off" class="layui-input">
                                </div>
                            </div>
                            <div class="layui-inline">
                                <label class="layui-form-label">校区编码</label>
                                <div class="layui-input-inline">
                                    <input type="text" name="campus_no" autocomplete="off" class="layui-input">
                                </div>
                            </div>
                            <div class="layui-inline">
                                <label class="layui-form-label">校区电话</label>
                                <div class="layui-input-inline">
                                    <input type="text" name="phone" autocomplete="off" class="layui-input">
                                </div>
                            </div>
                            <div class="layui-inline">
                                <button type="submit" class="layui-btn layui-btn-primary" lay-submit
                                    lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                            </div>
                        </div>
                    </form>
                </div>
            </fieldset>

            <script type="text/html" id="toolbarDemo">
                <div class="layui-btn-container">
                    <button class="layui-btn layui-btn-sm" style="background-color: #393D49;" lay-event="refresh"><i class="fa fa-refresh layui-anim-rotate"></i></button>
                    <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> <i class="fa fa-plus"></i> 添加 </button>
                    <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn layui-btn-disabled" id="delete" lay-event="delete"> <i class="fa fa-close"></i> 删除 </button>
                    <button class="layui-btn layui-btn-sm layui-btn-danger" lay-event="recycle"><i class="fa fa-recycle"></i> 回收站 </button>
                </div>
            </script>

            <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

            <script type="text/html" id="currentTableBar">
                <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit"><i class="fa fa-pencil"></i></a>
                <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete"><i class="fa fa-close"></i></a>
            </script>

        </div>
    </div>
    <script src="/static/crm/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
    <script>
        layui.use(['form', 'table', 'jquery', 'layer'], function () {
            var $ = layui.jquery,
                form = layui.form,
                layer = layui.layer,
                table = layui.table;

            table.render({
                elem: '#currentTableId',
                url: '/crm/product.campus/index',
                toolbar: '#toolbarDemo',
                defaultToolbar: ['filter', 'exports', 'print', {
                    title: '搜索',
                    layEvent: 'search',
                    icon: 'layui-icon-search'
                }],
                cols: [[
                    { type: "checkbox", width: 50, fixed: 'left' },
                    { field: 'id', width: 60, title: 'ID', sort: true },
                    { field: 'name', title: '校区名称' },   
                    { field: 'campus_no', title: '校区编码' },   
                    { field: 'phone', title: '校区电话' },   
                    { field: 'address', title: '校区地址' },   
                    { field: 'create_time', width: 160, title: '创建时间' },
                    { field: 'update_time', width: 160, title: '更新时间' },
                    { title: '操作', minWidth: 80, width: 90, toolbar: '#currentTableBar', align: "center", fixed: 'right' }
                ]],
                limits: [10, 15, 20, 25, 50, 100],
                limit: 10,
                page: true,
            });

            // 监听搜索操作
            form.on('submit(data-search-btn)', function (data) {
                // console.log(data.field);
                //执行搜索重载
                table.reload('currentTableId', {
                    page: { curr: 1 },
                    where: data.field
                }, 'data');
                return false;
            });

            // toolbar监听事件
            table.on('toolbar(currentTableFilter)', function (obj) {
                if (obj.event === 'add') {  // 监听添加操作
                    openLayer('/crm/product.campus/add', '添加校区');
                } else if (obj.event === 'search') {   // 监听搜索操作
                    $(".table-search-fieldset").toggle();
                } else if (obj.event === 'recycle') {  // 监听回收站操作
                    openLayer('/crm/product.campus/recycle', '回收站');
                } else if (obj.event === 'refresh') {  // 监听刷新操作
                    table.reload('currentTableId', {
                        page: { curr: 1 }
                    });
                } else if (obj.event === 'delete') {  // 监听批量删除操作
                    var checkStatus = table.checkStatus('currentTableId'),
                        data = checkStatus.data,
                        ids = '';
                    if (data.length > 0) {  // 获取多条删除id
                        for (const i in data) ids += data[i].id + ',';
                    }
                    layer.confirm('是否删除已选中数据', function (index) {
                        $.ajax({
                            url: '/crm/product.campus/del?id=' + ids,
                            dataType: 'json',
                            success: function (res) {
                                if (res.code == 1) {
                                    layer.msg(res.msg);
                                    layer.close(index);
                                    table.reload('currentTableId', {  // 'table' 为table的 id
                                        scrollPos: 'fixed'  // 保持滚动条位置不变 - v2.7.3 新增
                                    });
                                }
                                layer.msg(res.msg);
                            }
                        });
                    });
                }
            });

            //监听表格复选框选择
            table.on('checkbox(currentTableFilter)', function (obj) {
                $('.layui-form-checked').length >= 1 ? $("#delete").removeClass("layui-btn-disabled") : $("#delete").addClass("layui-btn-disabled");
            });

            table.on('tool(currentTableFilter)', function (obj) {
                var data = obj.data,
                    _id = data.id;   // 获取id

                if (obj.event === 'edit') {  // 监听修改操作
                    openLayer('/crm/product.campus/edit?id=' + _id, '编辑校区');
                } else if (obj.event === 'delete') {
                    layer.confirm('确定删除吗？', function (index) {
                        $.ajax({
                            url: '/crm/product.campus/del?id=' + _id,
                            dataType: 'json',
                            success: function (res) {
                                if (res.code == 1) {
                                    layer.msg(res.msg);
                                    obj.del(); // 删除对应行（tr）的 DOM 结构，并更新缓存
                                    layer.close(index);
                                    table.reload('currentTableId', {  // 'table' 为table的 id
                                        scrollPos: 'fixed'  // 保持滚动条位置不变 - v2.7.3 新增
                                    });
                                }
                                layer.msg(res.msg);
                            }
                        });
                    });
                }
            });

            // 弹出层
            function openLayer(url, title) {
                var index = layer.open({
                    title: title,
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['95%', '90%'],
                    content: url,
                    end: function () { // 自动重载
                        table.reload('currentTableId', {
                            scrollPos: 'fixed'
                        });
                    }
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            }

        });
    </script>

</body>

</html>